package cn.fanyamin.jms;

import java.io.File;
import java.io.IOException;
import java.net.URI;

import org.apache.activemq.broker.BrokerFactory;
import org.apache.activemq.broker.BrokerService;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import cn.fanyamin.server.AbstractService;

/**
 * The Class JmsService.
 */
public class JmsService extends AbstractService {

    /** The Constant MDP_JMS_SERVICE. */
    private static final String JMS_SERVICE = "JmsService";

    /** The broker. */
    private BrokerService broker;

    /** The data dir. */
    private String mqDataDir;
    
    /** The lock file. */
    private String mqLockFile = "kahadb/lock";

    /** The config file. */
    private String mqConfigFile = "xbean:activemq.xml";
    /**
     * Instantiates a new jms service2.
     */
    public JmsService() {

    }

    /**
     * Gets the name.
     *
     * @return name
     */
    public String getName() {
        if (null != this.getBrokerService()) {
            this.getBrokerService().getBrokerName();
        }
        return JMS_SERVICE;
    }

    /**
     * Gets the broker service.
     * 
     * @return the broker service
     */
    public BrokerService getBrokerService() {
        return this.broker;
    }

    /**
     * Inits the.
     *
     * @throws Exception e
     */
    public void onInit() throws Exception {        
        if (broker == null) {
            if (StringUtils.isNotBlank(mqDataDir) && StringUtils.isNotBlank(mqLockFile)) {
                releaseFileLock();
            }
            broker = BrokerFactory.createBroker(new URI(mqConfigFile));
        }
    }

    /**
     * Release file lock.
     *
     * @throws IOException delete file error
     */
    private void releaseFileLock() throws IOException {
        // delete lock file if cannot acquire file lock
        // {activemq.broker.datadir}/kahadb/lock        
        String lockFilePath = mqDataDir + "/" + mqLockFile;
        File aLockFile = new File(lockFilePath);
        if (null != aLockFile && aLockFile.exists()) {
            FileUtils.forceDelete(aLockFile);
        }
    }

    /**
     * Clean.
     *
     * @throws Exception e
     */
    public void onClean() throws Exception {
        if (broker != null && broker.isStarted()) {
            broker.stop();
        }
    }

    /**
     * To string.
     *
     * @return a string
     */
    public String toString() {
        return broker.getSystemUsage().getStoreUsage().toString();
    }

   

    /**
     * Start.
     *
     * @throws Exception e
     */
    public void onStart() throws Exception {
        if (broker == null) {
            throw new IllegalStateException(
                    "broker should be created successfully");
        }
        broker.start();
        broker.waitUntilStarted();

    }

    /**
     * Stop.
     *
     * @throws Exception e
     */
    public void onStop() throws Exception {
        if (broker == null) {
            throw new IllegalStateException(
                    "broker should be created successfully");
        }
        broker.stop();
        broker.waitUntilStopped();
    }

    /**
     * @param mqDataDir the mqDataDir to set
     */
    public void setMqDataDir(String mqDataDir) {
        this.mqDataDir = mqDataDir;
    }

    /**
     * @param mqLockFile the mqLockFile to set
     */
    public void setMqLockFile(String mqLockFile) {
        this.mqLockFile = mqLockFile;
    }

    /**
     * @param mqConfigFile the mqConfigFile to set
     */
    public void setMqConfigFile(String mqConfigFile) {
        this.mqConfigFile = mqConfigFile;
    }

    

}
